package com.jrzh.task;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jrzh.common.tools.CollectionTools;
import com.jrzh.config.sys.SysConfigration;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.mvc.model.sys.EmailSendingModel;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;

@Component
public class SendEmailTask {
	
	Log log = LogFactory.getLog(getClass());
	
	@Autowired
	private SysConfigration sysConfigration;
	
	@Autowired
	private SysServiceManage sysServiceManage;
	
	//@Scheduled(cron = "0/15 * * * * ?")
	public void send(){
		try {
				log.info("####sys模块定时邮件扫描#####");
				if(!sysConfigration.isSendMessage.equals("true"))return;
				log.info("####sys模块定时邮件发送执行#####");
				//发送邮件
				List<EmailSendingModel> emailList = sysServiceManage.emailSendingService.top(10);
				if(CollectionTools.isNotBlank(emailList)){
					for(EmailSendingModel sending : emailList){
						try {
							sysServiceManage.emailSendService.saveSend(sending);
						} catch (Exception e) {
							Integer tryTimes = sending.getTryTimes() == null ? 0 : sending.getTryTimes();
							sending.setTryTimes(tryTimes + 1);
							sysServiceManage.emailSendingService.edit(sending, SessionUser.getSystemUser());
							e.printStackTrace();
						}
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
